//
//  PositionSelectView.m
//  CheckInforDemo
//
//  Created by bob on 2017/8/10.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "PositionSelectView.h"
#import "AreaDataModel.h"
#import "GroupListModel.h"
#import "ShopTypeModel.h"
#import "SubjiectListModel.h"
#import "ProductTypeModel.h"
#import "ExaminationModel.h"
@interface PositionSelectView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,strong)NSMutableArray *dataArr;

@property(nonatomic,assign)BOOL isEmpty;//是否为空

@property(nonatomic,copy)NSString*PostName;

@property(nonatomic,copy)NSString*Postid;

@property(nonatomic,assign)SeletionType Mtype;

@property(nonatomic,strong)NSDictionary *paradict;
@end

@implementation PositionSelectView


-(instancetype)initWithFrame:(CGRect)frame andType:(SeletionType)type andArr:(NSArray*)ChooseArr andparam:(NSDictionary*)dict
{
    
    if (self=[super initWithFrame:frame]) {
        
        
        self= [[[NSBundle mainBundle]loadNibNamed:@"PositionSelectView" owner:self options:nil]lastObject];
        
         self.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
        self.PostionPickView.delegate=self;
        
        self.PostionPickView.dataSource=self;

        self.isEmpty=NO;
        
        self.dataArr=[NSMutableArray array];
        
        if (ChooseArr.count!=0) {
            
            [self.dataArr addObjectsFromArray:ChooseArr];
        }
  
        self.TipBtn.hidden=YES;
  
        [self.ActivityIndicator startAnimating];
        
        self.ActivityIndicator.hidesWhenStopped=YES;
        
        self.Mtype=type;
        
        self.paradict=dict;
        
      if (type==GetArealist)
        {
            [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/areas.json" andParams:dict andBoolToken:NO];
            
        }else if (type==GetshopsGroup)
        {
            
             [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/groupList.json" andParams:dict andBoolToken:YES];
            
        }else if (type==GetTypelist)
        {
   
             [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/shopType.json" andParams:dict andBoolToken:NO];
            
        }else if (type==GetSubjectList)
        {
             [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/inspectSubjectList.json" andParams:dict andBoolToken:YES];
            
        }else if (type==AdditiveType)
        {
            [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/additiveCatList.json" andParams:dict andBoolToken:YES];
        }else if (type==ProductType)
        {
            [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/commonProductTypeList.json" andParams:dict andBoolToken:YES];
        }else if (type==ExaminationType)
        {
            [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/examList.json" andParams:dict andBoolToken:YES];
        }
    }
    
    return self;
}

-(void)RequestDatesWithArmStr:(NSString*)reqstr andParams:(NSDictionary*)params andBoolToken:(BOOL)isToken
{

    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:reqstr andrequestTime:5 andParams:params andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:isToken];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {

        
        if (responseStatus==1) {
            
             [self.ActivityIndicator stopAnimating];
            if (self.Mtype==GetArealist) {
                
                AreaDataModel *Areadata=[AreaDataModel mj_objectWithKeyValues:responseObject];
         
                //请求成功
                if (Areadata.data.count==0) {
                    
                    self.TipBtn.hidden=NO;
                    
                    self.isEmpty=YES;
                    
                    [self.TipBtn setTitle:@"没有内容" forState:UIControlStateNormal];
                    
                }else{
                    
                    
                    [self.dataArr addObjectsFromArray:Areadata.data];
                 
                    [self.PostionPickView reloadAllComponents];
                    
                    AreaData *model=self.dataArr[0];
                    
                    self.Postid=[NSString stringWithFormat:@"%ld",model.areaId];
                    
                    self.PostName=model.areaName;
                    
                }
                
            }else if (self.Mtype==GetshopsGroup)
            {
                GroupListModel *GroupData=[GroupListModel mj_objectWithKeyValues:responseObject];
                
                 
                //请求成功
                if (GroupData.data.count==0) {
                    
                    self.TipBtn.hidden=NO;
                    
                    self.isEmpty=YES;
                    
                    [self.TipBtn setTitle:@"没有内容" forState:UIControlStateNormal];
         
                }else{
                    
                    [self.dataArr addObjectsFromArray:GroupData.data];
                    
                  
                    
                    [self.PostionPickView reloadAllComponents];
                    
                    GroupListData *model=self.dataArr[0];
                    
                    self.Postid=[NSString stringWithFormat:@"%ld",model.groupId];
                    
                    self.PostName=model.groupName;
                    
                }
                
            }else if (self.Mtype==GetSubjectList)
            {
                
                
                 SubjiectListModel *TypeData=[SubjiectListModel mj_objectWithKeyValues:responseObject];
        
                //请求成功
                if (TypeData.data.count==0) {
                    
                    self.TipBtn.hidden=NO;
                    
                    self.isEmpty=YES;
                    
                    [self.TipBtn setTitle:@"没有内容" forState:UIControlStateNormal];
                    
                }else{
                    
                    [self.dataArr addObjectsFromArray:TypeData.data];
             
                    [self.PostionPickView reloadAllComponents];
                    
                     SubjectData *model=self.dataArr[0];
                    
                    self.Postid=[NSString stringWithFormat:@"%ld",model.subjectId];
                    
                    self.PostName=model.subjectName;
                    
                }
         
            }else if (self.Mtype==GetTypelist)
            {
                
                ShopTypeModel *TypeData=[ ShopTypeModel mj_objectWithKeyValues:responseObject];
                ShopTypeData *shopMode = [ShopTypeData new];
                shopMode.name = @"全部";
                shopMode.ID = 0;
              
                //请求成功
                if (TypeData.data.count==0) {
                    
                    self.TipBtn.hidden=NO;
                    
                    self.isEmpty=YES;
                    
                    [self.TipBtn setTitle:@"没有内容" forState:UIControlStateNormal];
                    
                }else{
                    
                    [self.dataArr addObjectsFromArray:TypeData.data];
                    [self.dataArr addObject:shopMode];
                    
                    [self.PostionPickView reloadAllComponents];
                    
                    ShopTypeData *model=self.dataArr[0];
                    
                    self.Postid=[NSString stringWithFormat:@"%ld",model.ID];
                    
                    self.PostName=model.name;
                    
                }
                
            }else if (self.Mtype == AdditiveType)
            {
                ShopTypeModel *TypeData=[ ShopTypeModel mj_objectWithKeyValues:responseObject];
                
                //请求成功
                if (TypeData.data.count==0) {
                    
                    self.TipBtn.hidden=NO;
                    
                    self.isEmpty=YES;
                    
                    [self.TipBtn setTitle:@"没有内容" forState:UIControlStateNormal];
                    
                }else{
                    
                    [self.dataArr addObjectsFromArray:TypeData.data];
                    
                    [self.PostionPickView reloadAllComponents];
                    
                    ShopTypeData *model=self.dataArr[0];
                    
                    self.Postid=[NSString stringWithFormat:@"%ld",model.ID];
                    
                    self.PostName=model.name;
                    
                }
            }else if (self.Mtype==ProductType)
            {
                ProductTypeModel *TypeData=[ProductTypeModel mj_objectWithKeyValues:responseObject];
                
                //请求成功
                if (TypeData.data.count==0) {
                    
                    self.TipBtn.hidden=NO;
                    
                    self.isEmpty=YES;
                    
                    [self.TipBtn setTitle:@"没有内容" forState:UIControlStateNormal];
                    
                }else{
                    
                    [self.dataArr addObjectsFromArray:TypeData.data];
                    
                    [self.PostionPickView reloadAllComponents];
                    
                    ProductTypeData *model=self.dataArr[0];
                    
                    self.Postid=[NSString stringWithFormat:@"%zd",model.catId];

                    self.PostName=model.catName;
                    
                }
            }else if (self.Mtype == ExaminationType)
            {
                ExaminationModel *InforModel=[ExaminationModel mj_objectWithKeyValues:responseObject];
                
                //请求成功
                if (InforModel.data.count==0) {
                    
                    self.TipBtn.hidden=NO;
                    
                    self.isEmpty=YES;
                    
                    [self.TipBtn setTitle:@"没有内容" forState:UIControlStateNormal];
                    
                }else{
                    
                    [self.dataArr addObjectsFromArray:InforModel.data];
                    
                    [self.PostionPickView reloadAllComponents];
                    
                    ExaminationData *model=self.dataArr[0];
                    
                    self.Postid=model.ID;
                    
                    self.PostName=model.name;
                    
                }
            }
            
           
        }else if (responseStatus==0||responseStatus==2)
        {
             [self.ActivityIndicator stopAnimating];
            
            self.TipBtn.hidden=NO;
            
            [self.TipBtn setTitle:@"请求失败，请点击重试" forState:UIControlStateNormal];
            
        }else{
            //404需要重新登录了
            
             [self.ActivityIndicator stopAnimating];
            
            self.TipBtn.hidden=NO;
            
            [self.TipBtn setTitle:@"请求失败，请点击重试" forState:UIControlStateNormal];
            
        }
        
        
    }];
    

}


#pragma mark-窗口选择代理方法
//获取UIPickerView指定列中包含的列表项的数量
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    
    return 1;
}


//获取UIPickerView包含的列数量
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    
    return self.dataArr.count;
    
}


//列表的宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    
    
    return 300;
    
}

//列表项的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    
    return 45;
    
}

//列表的内容
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    if (self.Mtype==GetArealist){
        
        AreaData *model=self.dataArr[row];
        
        return model.areaName;
        
    }else  if (self.Mtype==GetshopsGroup){
   
        GroupListData *model=self.dataArr[row];
     
        return model.groupName;
        
    }else if (self.Mtype==GetSubjectList)
    {
     
        SubjectData *model=self.dataArr[row];
        
        return model.subjectName;
        
        
    }else if (self.Mtype==GetISAnswer)
    {
        NSString *name=self.dataArr[row];
        
        return name;
    }else if (self.Mtype==ProductType)
    {
        ProductTypeData *model = self.dataArr[row];
        return model.catName;
    }
    else{
        ShopTypeData *model=self.dataArr[row];
        
        return model.name;
    }
 
}


//点击时选择项
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
     if (self.dataArr.count==0) {
         
         return;
         
     }
    
   if (self.Mtype==GetArealist){
         
         AreaData *model=self.dataArr[row];
         
         self.Postid=[NSString stringWithFormat:@"%ld",model.areaId];
         
         self.PostName=model.areaName;
         
     }else if (self.Mtype==GetshopsGroup)
     {
         
         GroupListData *model=self.dataArr[row];
         
         self.Postid=[NSString stringWithFormat:@"%ld",model.groupId];
         
         self.PostName=model.groupName;
         
     }else if (self.Mtype==GetTypelist)
     {
         ShopTypeData *model=self.dataArr[row];
         
         self.Postid=[NSString stringWithFormat:@"%ld",model.ID];
         
         self.PostName=model.name;
         
     }else if (self.Mtype==GetSubjectList)
     {
        SubjectData *model=self.dataArr[row];
         
         self.Postid=[NSString stringWithFormat:@"%ld",model.subjectId];
         
         self.PostName=model.subjectName;
         
     }else if (self.Mtype == AdditiveType)
     {
         ShopTypeData *model=self.dataArr[row];
         
         self.Postid=[NSString stringWithFormat:@"%ld",model.ID];
         
         self.PostName=model.name;
     }else if (self.Mtype == ProductType)
     {
         ProductTypeData *model=self.dataArr[row];
         
         self.Postid=[NSString stringWithFormat:@"%ld",model.catId];
         
         self.PostName=model.catName;
     }else if (self.Mtype == ExaminationType)
     {
         ExaminationData *model=self.dataArr[row];
         
         self.Postid=model.ID;
         
         self.PostName=model.name;
     }
}
- (IBAction)RetryAction:(UIButton *)sender {
    
    self.TipBtn.hidden=YES;
    
    [self.ActivityIndicator startAnimating];
    
    if (self.Mtype==GetArealist)
    {
        [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/areas.json" andParams:self.paradict andBoolToken:NO];
        
    }else if (self.Mtype==GetshopsGroup)
    {
        
        [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/groupList.json" andParams:self.paradict andBoolToken:YES];
        
    }else if (self.Mtype==GetTypelist)
    {
            [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/shopType.json" andParams:self.paradict andBoolToken:NO];
        
    }else if (self.Mtype==GetSubjectList)
    {
        [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/inspectSubjectList.json" andParams:self.paradict andBoolToken:YES];
        
    }else if (self.Mtype==AdditiveType)
    {
        [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/additiveCatList.json" andParams:self.paradict andBoolToken:YES];
    }else if (self.Mtype==ProductType)
    {
        [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/commonProductTypeList.json" andParams:self.paradict andBoolToken:YES];
    }else if (self.Mtype==ExaminationType)
    {
        [self RequestDatesWithArmStr:@"/index.php/FoodCheckApi/examList.json" andParams:self.paradict andBoolToken:YES];
    }

}

-(void)PopWithView:(UIView*)SupView
{
    
    [SupView addSubview:self];
    
    POPBasicAnimation *BaseA=[POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    
    BaseA.fromValue=@(0.0);
    
    BaseA.toValue=@(0.3);
    
    BaseA.duration=0.3;
    
    [self.bagView pop_addAnimation:BaseA forKey:@"fade"];
    
    POPBasicAnimation *BaseB=[POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
    
    BaseB.fromValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height+self.ContentView.height/2.0)];
    
    BaseB.toValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height-self.ContentView.height/2.0)];
    
    BaseB.duration=0.3;
    
    [self.ContentView pop_addAnimation:BaseB forKey:@"dy"];

}


-(void)dissminsView
{
    
    POPBasicAnimation *BaseA=[POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    
    BaseA.fromValue=@(0.3);
    
    BaseA.toValue=@(0.0);
    
    BaseA.duration=0.3;
    
    [self.bagView pop_addAnimation:BaseA forKey:@"fade"];
    
    POPBasicAnimation *BaseB=[POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
    
    BaseB.fromValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height-self.ContentView.height/2.0)];
    
    BaseB.toValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height+self.ContentView.height/2.0)];
    
    BaseB.duration=0.3;
    
    [self.ContentView.layer pop_addAnimation:BaseB forKey:@"dy"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self removeFromSuperview];
        
    });

    
    
}
- (IBAction)OkAction:(UIButton *)sender {
    
    
    if (self.dataArr.count!=0) {
        
        _block(self.PostName,self.Postid);
        
    }

    [self dissminsView];
    
}
- (IBAction)CancleAction:(UIButton *)sender {
    
    [self dissminsView];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
